let util = {
  formatTime:date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
    return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  },
  formatNumber: n => {
    n = n.toString()
    return n[1] ? n : '0' + n
  },

  //计算经纬度距离
  GetDistance: function (lat1, lng1, lat2, lng2) {
    var radLat1 = lat1 * Math.PI / 180.0;
    var radLat2 = lat2 * Math.PI / 180.0;
    var a = radLat1 - radLat2;
    var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
      Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
    s = s * 6378.137;// EARTH_RADIUS;
    s = Math.round(s * 10000) / 10000;
    return s;
  },
  // 页面跳转
  pageJump:(url,type) => {
    if (!url) {return};
    let navType = type || 'navigateTo';
    switch(navType){
      case "navigateTo":
        wx.navigateTo({
          url:url
        });
        break;
      case "redirectTo":
        wx.redirectTo({
          url:url
        });
        break;
      case "switchTab":
        wx.switchTab({
          url:url
        });
        break;
      case "reLaunch":
        wx.reLaunch({
          url:url
        });
        break;
      default :
        break;
    }                               
  },
  // 请求
  ajax:function(option,callback){
    // var urlStr = 'http://192.168.188.122:8080/';  //局域
    // var urlStr = 'http://192.168.188.140/bjf/public/';  //使用庆炼的电脑作为局域网服务器
    var urlStr = 'https://www.fjsmb.com/';   //线上
     // var urlStr = 'http://czk.jsshop.com/';    //本地

    let token = wx.getStorageSync('userInfo').token;
    var defaults = {
      method:'GET',
      header:{
        'content-type': 'application/x-www-form-urlencoded'
      },
      data:{},
      // dataType: "json",
      success:function(data){
        wx.hideLoading();
        if (data.data.code == 1) {
          callback && callback(data);
        }else if(data.data.code == -1){
           wx.setStorageSync('userInfo', '');
           wx.showModal({
            content:'登录超时，重新登录',
            success:function(data){
              if (data.confirm) {
                wx.switchTab({url:'/pages/my/my'});
              }
            }
          })
        }else{
          wx.showToast({
            title:data.data.msg || '请求失败',
            icon:"none"
          });
        }
      },
      fail:function(data){
        wx.hideLoading();
        wx.showToast({
          title:'加载失败',
          icon:'none'
        })
      }, 
      complete:function(data){
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    };
    for(var j in option){
      if (j == 'header') {
        continue;
      }
      defaults[j] = option[j];
    }
    if (option.header) {
      for(var k in option.header ){
        defaults.header[k] = option.header[k];
      }
    }
    if (token) {
      defaults.data.token = token;
    }
    defaults.url = urlStr + defaults.url;
    wx.showLoading({title:'加载中'});
    wx.request(defaults);
  },
  address: function (callback){
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        callback  && callback(res);
      },
      fail(res) {
        wx.showToast({
          title: "地理位置获取失败",
          icon: 'none',
        })
      }
    })
  }
}

module.exports = util;
